//
//  MenuViewController.m
//  Spa
//
//  Created by PangPond on 10/1/2556.
//  Copyright (c) พ.ศ. 2556 innova. All rights reserved.
//

#import "MenuViewController.h"
#import "AddMenuViewController.h"
#import "Menu.h"
@interface MenuViewController ()

@end

@implementation MenuViewController

@synthesize databaseUtility =_databaseUtility;
@synthesize menu = _menu;
@synthesize tableView = _tableView;
@synthesize keyDelete;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self loadView];
    [self viewDidLoad];
}

- (BOOL) hidesBottomBarWhenPushed {
    return YES;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.hidesBottomBarWhenPushed = YES;
    self.databaseUtility = [[DatabaseUtility alloc]initWithDatabaseName:@"Database"];
    self.menu = [self.databaseUtility fetchedResultsWithClass:@"Menu"withSortDescriptorString:@"name"];
    [self.tableView reloadData];
    UIBarButtonItem * settingButton = [[UIBarButtonItem alloc] initWithTitle:@"Add" style:UIBarButtonItemStylePlain target:self action:@selector(AddProduct:)];
    self.navigationItem.rightBarButtonItem = settingButton;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableViewDataSource


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.menu count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //////
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    //config xell
    
    Menu * Menus = [self.menu objectAtIndex:indexPath.row];
    cell.textLabel.text = [NSString stringWithFormat:@"%@",Menus.name];
    
    return cell;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    AddMenuViewController *addMenu = [[AddMenuViewController alloc] initWithNibName:@"AddMenuViewController" bundle:nil];
    Menu * menus = [self.menu objectAtIndex:indexPath.row];
    addMenu.keyMenuName = menus.name;
    addMenu.keyIf = @"1";
    [self presentViewController:addMenu animated:YES completion:nil];
}


//funtion



-(IBAction)AddProduct:(id)sender{
    AddMenuViewController *addMenu = [[AddMenuViewController alloc] initWithNibName:@"AddMenuViewController" bundle:nil];
    addMenu.keyIf = @"2";
    [self presentViewController:addMenu animated:YES completion:nil];
    
}


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    Menu * menuKey = [self.menu objectAtIndex:indexPath.row];
    keyDelete = menuKey.name;
    
    NSArray *menus = [self.databaseUtility fetchedResultsWithClass:@"Menu" withSortDescriptorString:@"name" withPredicateString:[NSString stringWithFormat:@"name == '%@'",keyDelete]];
    
    Menu *menu = [menus lastObject];
    [self.databaseUtility.managedObjectContext deleteObject:menu];
    [self.databaseUtility saveContext];
    [self viewDidLoad];
}

@end
